package org.luckystrophe.snowtoms.model.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import org.luckystrophe.snowtoms.R;
import org.luckystrophe.snowtoms.manager.Md5Maker;
import org.luckystrophe.snowtoms.model.JsonRequestPOST;
import org.luckystrophe.snowtoms.pref.UserPreference_;

import android.content.Context;
import android.net.Uri;

import com.googlecode.androidannotations.annotations.EBean;
import com.googlecode.androidannotations.annotations.RootContext;
import com.googlecode.androidannotations.annotations.res.StringRes;
import com.googlecode.androidannotations.annotations.sharedpreferences.Pref;

@EBean
public class RequestMessage {

	@RootContext
	Context context;

	@StringRes(R.string.api_method_postMessage)
	String apiMethod;

	@Pref
	UserPreference_ pref;

	public JSONObject tempFileUpload(Uri filePath, String comment, String name, int messageType, boolean checkedLocation)
			throws IOException, JSONException {
		InputStream is = null;
		String loginType = pref.loginType().get();
		long usrId = pref.usrId().get();
		long ts = System.currentTimeMillis();
		String fingerPrint = Md5Maker.encryptString("snowtoms", ts);
		String collaboId = pref.collaboId().get();
		HttpClient client = new DefaultHttpClient();
		String postURL = JsonRequestPOST.REMOTE_SNOWTOMS;
		HttpPost post = new HttpPost(postURL);
		post.setHeader("ACCEPT-Charset", "UTF-8");
		MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null,
				Charset.forName("UTF-8"));
		if (filePath != null) {
			File file = new File(filePath.getPath());
			FileBody bin = new FileBody(file);
			reqEntity.addPart("image", bin);
		}
		if (checkedLocation) {
			reqEntity.addPart("location", new StringBody(pref.location().get(), Charset.forName("UTF-8")));
		}
		reqEntity.addPart("method", new StringBody(apiMethod));
		reqEntity.addPart("usrId", new StringBody(String.valueOf(usrId)));
		reqEntity.addPart("comment", new StringBody(comment, Charset.forName("UTF-8")));
		reqEntity.addPart("collaboId", new StringBody(collaboId));
		reqEntity.addPart("loginType", new StringBody(loginType, Charset.forName("UTF-8")));
		reqEntity.addPart("name", new StringBody(name, Charset.forName("UTF-8")));
		reqEntity.addPart("messageType", new StringBody(String.valueOf(messageType), Charset.forName("UTF-8")));
		reqEntity.addPart("fingerPrint", new StringBody(fingerPrint, Charset.forName("UTF-8")));
		reqEntity.addPart("timeStamp", new StringBody(String.valueOf(ts), Charset.forName("UTF-8")));
		reqEntity.addPart("fpKey", new StringBody("snowtoms", Charset.forName("UTF-8")));
		post.setEntity(reqEntity);

		HttpResponse response = client.execute(post);
		HttpEntity entityResponse = response.getEntity();

		is = entityResponse.getContent();

		BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
		StringBuilder sb = new StringBuilder();
		String line = null;
		while ((line = reader.readLine()) != null) {
			sb.append(line).append("\n");
		}
		is.close();

		JSONObject jo = new JSONObject(sb.toString());
		return jo;
	}
}
